﻿package com.devilcat.dt{
	import com.devilcat.dt.Movedt;
	import com.devilcat.dt.Itemdt;
	import com.devilcat.dt.Gamedt;
	import com.devilcat.ob.Pokemonob;
	import flash.display.MovieClip;

	public class Pokemondt {
		public var AttackMC:MovieClip;
		public var pid:Number;
		public var upid:Number;
		public var uid:Number;
		public var pmod:Number;
		public var Name:String;
		public var Type1:String;
		public var Type2:String;
		public var Ability:String;
		public var HP:Number;
		public var CurrentHP:Number;
		//public var OldHP:Number;
		public var Attack:Number;
		public var Defense:Number;
		public var SpAtk:Number;
		public var SpDef:Number;
		public var Speed:Number;
		public var Accuracy:Number=95;
		public var Evasive:Number=5;
		//public var Total:Number;
		public var Move1:Movedt;
		public var Move2:Movedt;
		public var Move3:Movedt;
		public var Move4:Movedt;
		public var Exp:XMLList;
		public var Rate:Number=6;
		public var Status:String="";
		public var StatusLost:Number=0;
		public var StatusTurn:Number=0;
		public var weatherAbi:Boolean=false;
		public var restoresHPEachTurnCount:Number=0;
		public var restoresHPEachTurnVaule:Number=0;		

		const BUG = 0;
		const DARK = 1;
		const DRAGON = 2;
		const ELECTRIC = 3;
		const FIGHTING = 4;
		const FIRE = 5;
		const FLYING = 6;
		const GHOST = 7;
		const GRASS = 8;
		const GROUND = 9;
		const ICE = 10;
		const NORMAL = 11;
		const POISON = 12;
		const PSYCHIC = 13;
		const ROCK = 14;
		const STEEL = 15;
		const WATER = 16;
		const NONE = 17;
		public function SetAttackMC(_at:MovieClip){
			AttackMC=_at;
		}
		public function Pokemondt(){
		}
		public function getBase(_Name:String){
			var baseXML:XMLList = Gamedt.pokemonsXML[_Name];
			Name=_Name;
			pid=baseXML.No;
			Type1=baseXML.Type1;
			Type2=baseXML.Type2;
			Ability=baseXML.Abi;
			HP=baseXML.HP;
			Attack=baseXML.Attack;
			Defense=baseXML.Defense;
			SpAtk=baseXML.SpAtk;
			SpDef=baseXML.SpDef;
			Speed=baseXML.Speed;						
			Exp=baseXML.Exp;
		}
		public function setMove(m1:String,m2:String,m3:String,m4:String){
			Move1 = new Movedt(m1);
			Move2 = new Movedt(m2);
			Move3 = new Movedt(m3);
			Move4 = new Movedt(m4);			
		}
		public function setPersonal(pokemon:Object){
			getBase(pokemon.Name);	
			Ability=pokemon.Ability;
			HP=pokemon.HP;
			CurrentHP=pokemon.CurrentHP;
			Attack=pokemon.Attack;
			Defense=pokemon.Defense;
			SpAtk=pokemon.SpAtk;
			SpDef=pokemon.SpDef;
			Speed=pokemon.Speed;
			Status=pokemon.Status;
			upid=pokemon.upid;
			setMove(pokemon.Move1,pokemon.Move2,pokemon.Move3,pokemon.Move4);
		}
		public function setWildData(pokemon:Object){
			getBase(pokemon.Name);	
			setMove(pokemon.Move1,pokemon.Move2,pokemon.Move3,pokemon.Move4);			
			if (String(pokemon.Rate)!="") {
				Rate=Number(pokemon.Rate);
			}
			if (String(pokemon.Evasive)!="") {
				Evasive=Number(pokemon.Evasive);
			}
			if (String(pokemon.Accuracy)!="") {
				Accuracy=Number(pokemon.Accuracy);
			}
			if(String(pokemon.Ability)!="") {
				Ability=pokemon.Ability;
			}
			addBonus(Number(pokemon.HP),Number(pokemon.Attack),Number(pokemon.Defense),Number(pokemon.SpAtk),Number(pokemon.SpDef),Number(pokemon.Speed));
			
		}
//		public function Pokemondt(Pokemon:XMLList,move1:XMLList,move2:XMLList,move3:XMLList,move4:XMLList) {
//		public function Pokemondt(Pokemon:XMLList,move1:XMLList,move2:XMLList,move3:XMLList,move4:XMLList) {
//			upid=-1;
//			pid=Pokemon.No;
//			pmod=Pokemon.pmod;
//			Name=Pokemon.name();
//			Type1=Pokemon.Type1;
//			Type2=Pokemon.Type2;
//			Ability=Pokemon.Abi;				
//			HP=Pokemon.HP;	
//			Status="";
//			Attack=Pokemon.Attack;
//			Defense=Pokemon.Defense;
//			SpAtk=Pokemon.SpAtk;
//			SpDef=Pokemon.SpDef;
//			Speed=Pokemon.Speed;	
//			Exp=Pokemon.Exp;
//			Recovery();		
//			Exp=Pokemon.Exp;
//			Move1=new Movedt(move1);
//			Move2=new Movedt(move2);
//			Move3=new Movedt(move3);
//			Move4=new Movedt(move4);
//		}
		public function Total():int{
			
			return Attack+Defense+SpAtk+SpDef+Speed;
		}
		
		public function GetMyPositionInList():Number{
			//Gamedt.Player.itemList
			for(var i:int=0;i<Gamedt.Player.pokemonList.length;i++){
				if(Gamedt.Player.pokemonList[i].upid==upid) return i;
			}
			return -1;
		}
		public function addBonus(_HP:Number,_Attack:Number,_Defense:Number,_SpAtk:Number,_SpDef:Number,_Speed:Number) {
			HP+=Number(_HP);
			Attack+=Number(_Attack);
			Defense+=Number(_Defense);
			SpAtk+=Number(_SpAtk);
			SpDef+=Number(_SpDef);
			Speed+=Number(_Speed);
			Recovery();		
		}
		public function getEXP(_HP:Number,_Attack:Number,_Defense:Number,_SpAtk:Number,_SpDef:Number,_Speed:Number) {
			HP+=Number(_HP);
			Attack+=Number(_Attack);
			Defense+=Number(_Defense);
			SpAtk+=Number(_SpAtk);
			SpDef+=Number(_SpDef);
			Speed+=Number(_Speed);
			Gamedt.updatePokemon.Update(this);
		}
		public function setStat(_HP:Number,_Attack:Number,_Defense:Number,_SpAtk:Number,_SpDef:Number,_Speed:Number) {
			HP=Number(_HP);
			Attack=Number(_Attack);
			Defense=Number(_Defense);
			SpAtk=Number(_SpAtk);
			SpDef=Number(_SpDef);
			Speed=Number(_Speed);
			//Total=Number(_Total);
			Recovery();		
		}

		public function Recovery(){			
			CurrentHP=HP;
			Status="";
		}
		public function restoresHPEachTurn(){
			if(restoresHPEachTurnCount>0){
				CurrentHP+=restoresHPEachTurnVaule;
				SetCorrectHP();
				restoresHPEachTurnCount--;
			}				
		}

//		public function checkAbility() {
//			recoveryMove();
//			switch (Ability) {
//				case "Adaptability" :
//					addBonusMoveAtkByAbility(Type1,1.2);
//					addBonusMoveAtkByAbility(Type2,1.2);
//					break;
//				case "Compoundeyes" :
//					Accuracy=100;
//					break;
//				case "HugePower" :
//					setNewTemStat("Attack",Attack*2);
//					break;
//				case "Hustle" :
//					setNewTemStat("Attack",Attack*1.5);
//					setMoveAccByCategory("Physical",0.8);
//					break;
//			}
//		}
		public function recoveryMove() {
			for (var i:Number=1; i<5; i++) {
				this["Move"+i].setDefaultValue();
			}
		}
		public function addBonusMoveAtkByAbility(mType:String, mBoot:Number) {
			for (var i:Number=1; i<5; i++) {
				if (this["Move"+i].Type==mType) {
					this["Move"+i].Attack=Math.round(this["Move"+i].Attack*mBoot);
				}
			}
		}
		public function setMoveAccByCategory(mCate:String, mRec:Number) {
			for (var i:Number=1; i<5; i++) {
				if (this["Move"+i].Category==mCate) {
					this["Move"+i].Accuracy=Math.round(this["Move"+i].Accuracy*mRec);
				}
			}
		}
		public function SetCorrectHP() {
			if (CurrentHP>HP) {
				CurrentHP=HP;
			}
			if (CurrentHP<0) {
				CurrentHP=0;
			}
		}
		public function IsStatusEffect():Boolean {
			var returnDamage:Number=0;
			var rndNumber:Number;
			if (Status!="") {
				switch (Status) {
					case "Sleep" :
						if (StatusTurn==0) {
							RemoveStatus();
							return false;
						} else {
							StatusTurn--;
						}
						break;
					case "Frozen" :

						rndNumber=Math.round(Math.random()*9);
						if (rndNumber==1) {
							RemoveStatus();
							return false;
						}
						break;
					case "Poison" :

						CurrentHP-=StatusLost;
						SetCorrectHP();
						break;
					case "Burn" :
						CurrentHP-=StatusLost;
						SetCorrectHP();
						break;
					case "Confuse" :
						rndNumber=Math.round(Math.random()*1);
						if (rndNumber==1) {
							var selfAttack:Number=Math.round(HP/7);
							CurrentHP-=selfAttack;
							SetCorrectHP();
							trace(Name+" " +Status+" -"+selfAttack);
							return true;
						} else {
							var conRandom:Number=Math.round(Math.random()*1);
							if (conRandom==1) {
								trace(Name+" End Confuse");
								RemoveStatus();
							}
							return false;
						}
						break;
					case "Parlyz" :
						rndNumber=Math.round(Math.random()*4);
						if (rndNumber>1) {
							return true;
						} else {
							var pazRandom:Number=Math.round(Math.random()*1);
							if (pazRandom==1) {
								trace(Name+" End Parlyz");
								RemoveStatus();
							}
							return false;
						}
						break;
				}
				return true;
			} else {
				return false;
			}
		}			

		public function UseItem(_item:Itemdt) {
			trace("Use "+_item.Name + " Type " +_item.Type);
			var isUsed:Boolean=false;
			switch(_item.Type){
				case "Potion":
					if(CurrentHP<HP){
						CurrentHP+=_item.Effect;		
						isUsed=true;
					}
					break;
				case "AddStat":
					this[_item.Stat]+=_item.Effect;
					isUsed=true;
					break;
			}			
			if(isUsed){
				_item.NumberOf--;
				SetCorrectHP();
				if(AttackMC!=null)AttackMC.ReText();
				Gamedt.updatePokemon.Update(this);
				Gamedt.updateItem.Update(_item);
			}
		}
		public function RemoveStatus() {
			Status="";
			StatusLost=0;
			StatusTurn=0;
			AttackMC.ReText();
		}
		public function calRate() {
			var _Total=Total();
			if (_Total<=300) {
				Rate=4;
			} else if (_Total<=400) {
				Rate=6;
			} else if (_Total<=500) {
				Rate=8;
			} else if (_Total<=600) {
				Rate=10;
			} else {
				Rate=12;
			}
		}
		public function setRate(newRate:Number) {
			if (newRate<1) {
				newRate=1;
			}
			Rate=newRate;
		}
		public function addRate(newRate:Number) {
			Rate+=newRate;
			if (Rate<1) {
				Rate=1;
			}
		}
		public function setLegend() {
			Rate=150;
		}
	}
}